package cn.hbu.zlfw.admin.dao.impl;

import java.util.List;
import java.lang.StringBuffer;
import java.util.Map;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import cn.hbu.zlfw.admin.dao.ScienceExpertDao;
import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.common.base.dao.impl.BaseDaoImpl;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.Utility;
/**
 * 
 * @ClassName: ScienceExpertDaoImpl 
 * @Description: 科技服务人员dao接口实现
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月27日 下午9:00:09 
 * @vsersion:v1.0
 */
@Repository("scienceExpertDao")
public class ScienceExpertDaoImpl extends BaseDaoImpl<ScienceExpert, String> implements ScienceExpertDao{
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.ScienceExpertDao#getScienceExpertByParam(java.util.Map)
	 */
	@Override
	public ScienceExpert getScienceExpertByParam(Map<String, Object> params) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("from ScienceExpert where ");
		for (Map.Entry<String, Object> param : params.entrySet()) {
			sbf.append(param.getKey()+"=:"+param.getKey()+" and ");
		}
		sbf.append(" delState=:delState ");
		params.put("delState", 0);
		Query query=this.getSession().createQuery(sbf.toString());
		return (ScienceExpert) Utility.queryParameterUtil(query, params).uniqueResult();
	}
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.ScienceExpertDao#listInviteScienceExpertByParams(cn.hbu.zlfw.common.util.PageModel, java.lang.String, java.lang.String)
	 */
	@Override
	public PageModel<ScienceExpert> listInviteScienceExpertByParams(PageModel<ScienceExpert> pageModel, String userName,
			 List<String> scienceExpertIds) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("scienceExpertIds", scienceExpertIds);
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		sbfhql.append("from ScienceExpert where id not in (:scienceExpertIds) ");
		sbfhqlCount.append("select count(*) from ScienceExpert where id not in (:scienceExpertIds)  ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
			sbfhqlCount.append("and userName like :userName ");
		}
		sbfhql.append(" and delState=:delState");
		sbfhqlCount.append(" and delState=:delState");
		params.put("delState", 0);
		return this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
	}
	/*
	 * 分页条件查询本单位所有科技服人员
	 */
	@Override
	public PageModel<ScienceExpert> listAllScienceByParamsToUnit(PageModel<ScienceExpert> pageModel, String userName,
			Integer schoolType) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("schoolType", schoolType);
		StringBuffer sbfhql = new StringBuffer();
		sbfhql.append("from ScienceExpert where schoolType=:schoolType ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
		}
		sbfhql.append(" and delState=:delState");
		params.put("delState", 0);
		return this.findListByParams(sbfhql.toString(), null, params, pageModel);
	}
	/**
	 * 单位管理员查询本单位所有科技服务人员信息
	 */
	@Override
	public PageModel<ScienceExpert> listScienceExpertByParamsToUnit(PageModel<ScienceExpert> pageModel,String userName, String department,
			Integer schoolType) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("schoolType", schoolType);
		params.put("delState", 0);	//只查询"未删除"科技服务人员
		StringBuffer sbfhql = new StringBuffer();
		sbfhql.append("from ScienceExpert where schoolType=:schoolType and delState=:delState  ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
		}
		if(department != null && department != "") {
			params.put("department", department);
			sbfhql.append("and department = :department ");
		}
		return this.findListByParams(sbfhql.toString(), null, params, pageModel);
	}
	@Override
	public ScienceExpert getScienceExpertToUpdate(String phoneNumber, String id) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("id", id);
		params.put("phoneNumber", phoneNumber);
		params.put("delState", 0);	//只查询"未删除"科技服务人员
		String hql = "from ScienceExpert where id != :id and phoneNumber=:phoneNumber and delState=:delState";
		Query query = this.getSession().createQuery(hql);
		return (ScienceExpert) Utility.queryParameterUtil(query, params).uniqueResult();
	}

}
